package com.maila.xiaoaoschool.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.entity.ShopClasses;
import com.maila.xiaoaoschool.util.Constants;
import com.zhy.autolayout.utils.AutoUtils;

import java.text.NumberFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 优选商品adapter
 * Created by Administrator on 2017/9/26.
 */

public class AffirmOrderAdapter extends BaseAdapter {
    Context context;
    List<ShopClasses.DataBean.GoodBean> mdata;
    private NumberFormat nf;
    public AffirmOrderAdapter(Context context, List<ShopClasses.DataBean.GoodBean> mdata) {
        this.context = context;
        this.mdata = mdata;
        nf = NumberFormat.getCurrencyInstance();
        nf.setMaximumFractionDigits(2);
    }

    @Override
    public int getCount() {
        return mdata.size();
    }

    @Override
    public ShopClasses.DataBean.GoodBean getItem(int position) {
        return mdata.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_order_food, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
            AutoUtils.autoSize(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Glide.with(context).load(Uri.parse(Constants.URL_ROOT_XIAOAO + mdata.get(position).getImg())).into(holder.ivFood);
        holder.tvFoodName.setText(mdata.get(position).getGoodsname());
        holder.tvFoodCount.setText("*  "+mdata.get(position).getBuynum() );
        holder.tvFoodFee.setText(nf.format(mdata.get(position).getBuynum()*mdata.get(position).getCost()));
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.iv_food)
        ImageView ivFood;
        @BindView(R.id.tv_food_name)
        TextView tvFoodName;
        @BindView(R.id.tv_food_count)
        TextView tvFoodCount;
        @BindView(R.id.tv_food_fee)
        TextView tvFoodFee;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}